package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Device;
import com.ruoyi.system.domain.Parts;
import com.ruoyi.system.service.IPartsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 配件Controller
 *
 * @author wxd
 * @date 2024-08-25
 */
@Api(value = "配件", tags = {"配件"})
@RestController
@RequestMapping("/system/parts")
public class PartsController extends BaseController {
    @Autowired
    private IPartsService partsService;

    /**
     * 查询配件列表
     */
    @ApiOperation("查询分页列表")
    @GetMapping("/list")
    public TableDataInfo list(Parts parts) {
        startPage();
        List<Parts> list = partsService.selectPartsList(parts);
        return getDataTable(list);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @ApiOperation("查询字段分组列表")
    @Anonymous
    @GetMapping("/fieldGroup")
    public List<String> fieldGroup(String fieldName) {
        return partsService.selectFieldGroup(fieldName);
    }

    /**
     * 导出配件列表
     */
    @ApiOperation("导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Parts parts) {
        List<Parts> list = partsService.selectPartsList(parts);
        ExcelUtil<Parts> util = new ExcelUtil<Parts>(Parts.class);
        util.exportExcel(response, list, "配件数据");
    }

    /**
     * 获取配件详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(partsService.selectPartsById(id));
    }

    /**
     * 新增配件
     */
    @ApiOperation("新增")
    @PostMapping
    public AjaxResult add(@RequestBody Parts parts) {
        Parts old = partsService.selectPartsById(parts.getId());
        if(old!=null){
            return AjaxResult.error("已存在相同数据");
        }
        return toAjax(partsService.insertParts(parts));
    }

    /**
     * 修改配件
     */
    @ApiOperation("修改")
    @PutMapping
    public AjaxResult edit(@RequestBody Parts parts) {
        return toAjax(partsService.updateParts(parts));
    }

    /**
     * 删除配件
     */
    @ApiOperation("删除")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(partsService.deletePartsByIds(ids));
    }
}
